package org.kabeja.dxf.entity;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.helpers.DXFTextParser;
import org.kabeja.dxf.helpers.DXFUtils;
import org.kabeja.dxf.helpers.TextDocument;

/**
 * @author shutao
 * @since 2022/4/25 9:05
 */
public class DXFMText extends DXFText {
    public static final int ATTACHMENT_TOP_LEFT = 1;
    public static final int ATTACHMENT_TOP_CENTER = 2;
    public static final int ATTACHMENT_TOP_RIGHT = 3;
    public static final int ATTACHMENT_MIDDLE_LEFT = 4;
    public static final int ATTACHMENT_MIDDLE_CENTER = 5;
    public static final int ATTACHMENT_MIDDLE_RIGHT = 6;
    public static final int ATTACHMENT_BOTTOM_LEFT = 7;
    public static final int ATTACHMENT_BOTTOM_CENTER = 8;
    public static final int ATTACHMENT_BOTTOM_RIGHT = 9;
    private int attachmentpointLocation = 1;
    private double refwidth = 0.0D;
    private double refheight = 0.0D;

    public DXFMText() {
        this.setLineType(this.getType());
    }

    public void setAttachmentPoint(int var1) {
        this.attachmentpointLocation = var1;
    }

    public void setReferenceWidth(double var1) {
        this.refwidth = var1;
    }

    public double getReferenceWidth() {
        return this.refwidth;
    }

    public void setReferenceHeight(double var1) {
        this.refheight = var1;
    }

    public double getReferenceHeight() {
        return this.refheight;
    }

    @Override
    public String getType() {
        return DXFConstants.ENTITY_TYPE_MTEXT;
    }

    @Override
    public double getRotation() {
        if (this.rotation != 0.0D) {
            return this.rotation;
        } else if (this.align_p1.getX() == 0.0D && this.align_p1.getY() == 0.0D && this.align_p1.getZ() == 0.0D) {
            return this.rotation;
        } else {
            double[] var1 = new double[]{this.align_p1.getX(), this.align_p1.getY(), this.align_p1.getZ()};
            double var2 = this.align_p1.getX() / DXFUtils.vectorValue(var1);
            var2 = Math.toDegrees(Math.acos(var2));
            return var2;
        }
    }

    @Override
    public TextDocument getTextDocument() {
        return this.textDoc;
    }

    @Override
    public void setText(String var1) {
        this.text = var1;
        this.textDoc = DXFTextParser.parseDXFMText(this);
    }

    public int getAlignment() {
        return this.attachmentpointLocation;
    }

    @Override
    public boolean isOmitLineType() {
        return true;
    }

    @Override
    public Bounds getBounds() {
        Bounds var1 = new Bounds();
        int var2 = this.textDoc.getMaximumLineLength();
        if (var2 > 0) {
            double var3 = this.getHeight();
            if (var3 == 0.0D) {
                var3 = this.getReferenceHeight();
            }

            double var5 = (double)var2 * 0.7D * var3;
            var3 *= (double)this.textDoc.getLineCount();
            switch(this.attachmentpointLocation) {
                case 1:
                    var1.addToBounds(this.p.getX(), this.p.getY(), this.p.getZ());
                    var1.addToBounds(this.p.getX() + var5, this.p.getY() - var3, this.p.getZ());
                    break;
                case 2:
                    var1.addToBounds(this.p.getX() + var5 / 2.0D, this.p.getY(), this.p.getZ());
                    var1.addToBounds(this.p.getX() - var5 / 2.0D, this.p.getY() - var3, this.p.getZ());
                    break;
                case 3:
                    var1.addToBounds(this.p.getX(), this.p.getY(), this.p.getZ());
                    var1.addToBounds(this.p.getX() - var5, this.p.getY() - var3, this.p.getZ());
                    break;
                case 4:
                    var1.addToBounds(this.p.getX(), this.p.getY() + var3 / 2.0D, this.p.getZ());
                    var1.addToBounds(this.p.getX() + var5, this.p.getY() - var3 / 2.0D, this.p.getZ());
                    break;
                case 5:
                    var1.addToBounds(this.p.getX() + var5 / 2.0D, this.p.getY() + var3 / 2.0D, this.p.getZ());
                    var1.addToBounds(this.p.getX() - var5 / 2.0D, this.p.getY() - var3 / 2.0D, this.p.getZ());
                    break;
                case 6:
                    var1.addToBounds(this.p.getX(), this.p.getY() + var3 / 2.0D, this.p.getZ());
                    var1.addToBounds(this.p.getX() - var5, this.p.getY() - var3 / 2.0D, this.p.getZ());
                    break;
                case 7:
                    var1.addToBounds(this.p.getX() + var5, this.p.getY() + var3, this.p.getZ());
                    var1.addToBounds(this.p.getX(), this.p.getY(), this.p.getZ());
                    break;
                case 8:
                    var1.addToBounds(this.p.getX() + var5 / 2.0D, this.p.getY() + var3, this.p.getZ());
                    var1.addToBounds(this.p.getX() - var5 / 2.0D, this.p.getY(), this.p.getZ());
                    break;
                case 9:
                    var1.addToBounds(this.p.getX() - var5, this.p.getY() + var3, this.p.getZ());
                    var1.addToBounds(this.p.getX(), this.p.getY(), this.p.getZ());
            }
        } else {
            var1.setValid(false);
        }

        return var1;
    }
}
